package com.alm.token.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.utils.SpUtil;
import com.bumptech.glide.Glide;
import com.cryptape.cita_wallet.activity.AddWalletActivity;
import com.cryptape.cita_wallet.activity.FingerPrintActivity;
import com.cryptape.cita_wallet.activity.MainActivity;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.util.db.SharePrefUtil;

import static com.cryptape.cita_wallet.activity.SplashActivity.LOCK_TO_MAIN;

public class SplashActivity extends BaseActivity {

    @Override
    protected MyPreenter initPreenter() {
        return null;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initVariables() {
        if (ConStant.ISDEBUG) {
            int HOSTTYPE = SpUtil.getInt(MyApp.context, ConStant.HOSTTYPE, 1);
            ConStant.HOST_serv = SpUtil.getString(MyApp.context, ConStant.HOSTTYPE + 0, ConStant.HOST_serv);
            ConStant.HOST_test = SpUtil.getString(MyApp.context, ConStant.HOSTTYPE + 1, ConStant.HOST_test);
            ConStant.HOST_ip = SpUtil.getString(MyApp.context, ConStant.HOSTTYPE + 2, ConStant.HOST_ip);
            ConStant.HOST_ip1 = SpUtil.getString(MyApp.context, ConStant.HOSTTYPE + 3, ConStant.HOST_ip1);
            ConStant.HOST_ip2 = SpUtil.getString(MyApp.context, ConStant.HOSTTYPE + 4, ConStant.HOST_ip2);

            if (HOSTTYPE == 0) {
                ConStant.HOST = ConStant.HOST_serv;
            } else if (HOSTTYPE == 1) {
                ConStant.HOST = ConStant.HOST_test;
            } else if (HOSTTYPE == 2) {
                ConStant.HOST = ConStant.HOST_ip;
            } else if (HOSTTYPE == 3) {
                ConStant.HOST = ConStant.HOST_ip1;
            } else if (HOSTTYPE == 4) {
                ConStant.HOST = ConStant.HOST_ip2;
            }
        }
    }

    int delay = 1000;
    @Override
    protected void initViews() {
        ImageView imageView = (ImageView) findViewById(R.id.img_splash);
//        CommonUtil.GifListener gifListener = new CommonUtil.GifListener() {
//            @Override
//            public void gifPlayComplete() {
////                if (!TextUtils.isEmpty(SpUtil.getString(MyApp.context, ConStant.TOKEN, ""))
////                        && !TextUtils.isEmpty(SpUtil.getString(MyApp.context, ConStant.MEIQIAID, "")) && !SpUtil.getBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, false)) {
////                    startActivity(MainActivity.class);
////                } else {
////                    if (SpUtil.getBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, false)) {
////                        SpUtil.putBoolean(MyApp.context, ConStant.REMBERPSW, false);
////                    }
////                    startActivity(BlockWalletLoginActivity.class);
////                }
//
//            }
//        };
        Glide.with(this).load(R.drawable.splash_gif1).into(imageView);
        imageView.postDelayed(new Runnable() {
            @Override
            public void run() {
                gotoMainPage();
                finish();
            }
        }, delay);

//        Glide.with(this).load(R.drawable.splash_gif).into(new SimpleTarget<Drawable>() {
//            @Override
//            public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
//                if (drawable instanceof GifDrawable) {
//                    GifDrawable gifDrawable = (GifDrawable) drawable;
//                    gifDrawable.setLoopCount(1);
//                    imageView.setImageDrawable(drawable);
//                    gifDrawable.start();
//                    Field gifStateField = null;
//                    delay = 0;
//                    try {
//                        gifStateField = GifDrawable.class.getDeclaredField("state");
//                        gifStateField.setAccessible(true);
//                        Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
//                        Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
//                        gifFrameLoaderField.setAccessible(true);
//                        Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
//                        Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
//                        gifDecoderField.setAccessible(true);
//                        Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
//                        Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(gifDrawable)));
//                        Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
//                        getDelayMethod.setAccessible(true);
//
//                        //获得总帧数
//                        int count = gifDrawable.getFrameCount();
//                        for (int i = 0; i < count; i++) {
//                            //计算每一帧所需要的时间进行累加
//                            delay += (int) getDelayMethod.invoke(gifDecoder, i);
//                        }
//
//                    } catch (Exception e) {
//                        delay=0;
//                        e.printStackTrace();
//                        LogUtil.e("delay.e="+e.toString());
//                    } finally {
////                        LogUtil.e("delay="+delay);
//                        delay = delay == 0 ? 6770 : delay+2000;
////                        delay =   5000 ;
//                        imageView.postDelayed(new Runnable() {
//                            @Override
//                            public void run() {
//                                if (gifListener != null) {
//                                    gifListener.gifPlayComplete();
//                                }
//                            }
//                        }, delay);
//                    }
//
//                }
//            }
//        });

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
////                    Thread.sleep(6770);
//                    Thread.sleep(15000);
//                    if (!TextUtils.isEmpty(SpUtil.getString(MyApp.context, ConStant.TOKEN, ""))
//                            && !TextUtils.isEmpty(SpUtil.getString(MyApp.context, ConStant.MEIQIAID, "")) && !SpUtil.getBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, false)) {
//                        startActivity(MainActivity.class);
//                    } else {
//                        if (SpUtil.getBoolean(MyApp.context, ConStant.NEEDRELOGINANTTIME, false)) {
//                            SpUtil.putBoolean(MyApp.context, ConStant.REMBERPSW, false);
//                        }
//                        startActivity(BlockWalletLoginActivity.class);
//                    }
//                    finish();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }).start();
    }


    private void gotoMainPage() {
        if (!TextUtils.isEmpty(SharePrefUtil.getCurrentWalletName())) {
            if (SharePrefUtil.getBoolean(ConstantUtil.FINGERPRINT, false)) {
                Intent intent = new Intent(this, FingerPrintActivity.class);
                intent.putExtra(LOCK_TO_MAIN, true);
                startActivityForResult(intent,0);
            } else {
                startActivityForResult(new Intent(this, MainActivity.class),1);
            }
        } else {
            Intent intent = new Intent(this, AddWalletActivity.class);
            startActivityForResult(intent,2);
        }
    }
    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {

    }

}
